<script lang="ts">
import { ref, defineComponent, onMounted, watch } from "vue";
import { Pagination, Navigation } from "swiper";
import { Swiper, SwiperSlide } from "swiper/vue";
import "swiper/css";
import "swiper/css/pagination";
import "swiper/css/navigation";
import contact from "@/components/contact.vue";
import { Start, End } from "@/hook/animation";
import router from '@/router'
import resizeRank from '@/util/screen'

export default defineComponent({
  name: "swiper-example-loop-group",
  title: "Loop mode with multiple slides per group",
  url: import.meta.url,
  components: {
    Swiper,
    SwiperSlide,
    contact,
  },
  props: ["relatedData", "modalPruduct"],
  setup(props) {
    const relatedData = ref() as any;
    relatedData.value = props.relatedData;
    watch(
      () => props.relatedData,
      (val: any) => {
        relatedData.value = val;
      }
    );
    const onMouseEnter = (item: any) => {
      let dom1 = document.getElementById(item.id + "text");
      let dom2 = document.getElementById(item.id + "btn");

      Start(dom1, dom2);
    };
    const onMouseLeave = (item: any) => {
      let dom1 = document.getElementById(item.id + "text");
      let dom2 = document.getElementById(item.id + "btn");

      End(dom1, dom2);
    };
    const rank = ref(0); /* 1为手机端 */
    onMounted(() => {
      /**
       * @methods 判定pc&movie
       */
      rank.value = resizeRank(rank.value)
      window.addEventListener('resize', () => {
        let screenWidth = document.body.clientWidth
        if (screenWidth < 768) {
          rank.value = 1
        } else {
          rank.value = 0
        }
      })
    });
    const toProductDetail = (item: any) => {
      router.push({
        path: '/ProductDetail',
        query: {
          id: item.id
        }
      })
    }
    return {
      rank,
      onMouseEnter,
      onMouseLeave,
      relatedData,
      toProductDetail,
      modules: [Pagination, Navigation],
    };
  },
});
</script>
    
<template>
  <div class="content">
    <div class="relatedTitle bigTitle" v-if="relatedData?.length != 0">RELATED</div>
    <div class="relatedItem" v-if="relatedData?.length != 0">
      <swiper class="swiper" :modules="modules" :space-between="30" :slides-per-view="rank === 1 ? 1 : 3"
        :slides-per-group="rank === 1 ? 1 : 3" :pagination="{ clickable: true }">
        <swiper-slide v-for="item in relatedData" :key="item.id" @mouseenter="onMouseEnter(item)"
          @mouseleave="onMouseLeave(item)" @click="toProductDetail(item)">
          <el-card class="scaleImgCard relatedSlide" shadow="hover" style="min-height: 330px; border-radius: 15px">
            <div class="relatedSlideTag">
              <el-tag round color="#0071E3" v-if="item.tag">
                <!-- {{
                item.tag
              }} -->
              </el-tag>
            </div>
            <div class="relatedSlideImg">
              <img class="image_transform" :src="item.cover_image" alt="" />
            </div>
            <div class="contentText relatedSlideName">{{ item.name }}</div>
            <div :id="String(item.id) + 'text'" class="relatedSlideDemand auxiliaryText">
              {{ item.descr }}
            </div>
            <el-button :id="String(item.id) + 'btn'" class="relatedSlideButton btnStyle" color="#0071E3" size="large"
              round type="primary">点击查看</el-button>
          </el-card>
        </swiper-slide>
      </swiper>
    </div>

    <div class="relatedContact">
      <contact :modalPruduct="modalPruduct"></contact>
    </div>
  </div>
</template>
    
<style lang="scss" scoped>
@import url("@/assets/css/content.css");

.content {
  margin: 0 auto;
  padding: 60px 20px 40px 20px;

  .relatedTitle {
    text-align: center;
    padding-bottom: 40px;
  }
  .relatedItem {
    .swiper {
      height: 400px;
    }

    :deep(.el-card__body){
      padding: 15px 20px;
    }

    .relatedSlide {
      //   border: 1px solid #ccc;
      // background-color: #fff;
      // padding: 0 10px;
      // height: 300px;
      margin-top: 20px;
      text-align: center;
      border-radius: 20px;
      position: relative;
      border: none;

      .relatedSlideTag {
        position: absolute;
        height: 10%;
        display: flex;
        // padding-top: 8px;
        color: #fff;
      }

      .relatedSlideImg {
        width: 50%;
        // height: 50%;
        height: 150px;
        margin: 30px auto 0 auto;

        img {
          width: 100%;
          height: 100%;
          object-fit: contain;
        }
      }

      .relatedSlideName {
        font-size: $text;
        color: $auxiliaryColor;
        font-weight: bold;
        margin-top: 20px;
        // padding: 10px 0;
      }

      .relatedSlideDemand {
        position: absolute;
        top: 80%;
        left: 50%;
        transform: translate(-50%, -50%);
        opacity: 1;
        padding-top: 20px;
      }

      .relatedSlideButton {
        position: absolute;
        color: #fff;
        margin-top: 20px;
        opacity: 0;
        top: 80%;
        left: 50%;
        transform: translate(-50%, -50%);
      }
    }
  }

  .relatedContact {
    // margin-top: 60px;
    margin-bottom: 20px;
    // padding: 0 20px;
  }
}

:deep(.el-tag__content)  {
  color: #fff;
}

@media screen and (max-width: 768px) {
  .content {
    .relatedTitle {
      margin-top: 30px;
      text-align: left;
      // padding-left: 10px;
      padding-bottom: 20px;
    }

    .relatedItem {
      // padding: 0 20px;

      .relatedSlide {
        border: none;
        border-radius: 8px;
        // box-shadow: 0px 11px 35px 0px rgba(216, 222, 232, 0.55);
      }
    }

    .relatedContact {
      // padding: 0 20px 40px 20px;
      margin-bottom: 20px;
    }
  }
}
</style>